
#include "gtest/gtest.h"
#include "entitymanager.h"
#include "Entities/dummy.h"
#include "Game/Game.h"
#include <memory>
#include <Entities/plant.h>
#include <sdk/stats.h>

using namespace std;

TEST(basic_test, game_loop_test) {
    unique_ptr<Game> g(new Game());
    Entity *dummy = g->entityManager.get<entity::Dummy>();
    Actor *dummy1 = g->entityManager.get<entity::Dummy>();
    dummy1->setPos({2,1});
    Point test_point(2,1);
    EXPECT_EQ(dummy1->pos().x,test_point.x);
    EXPECT_EQ(dummy1->pos().y,test_point.y);
    dummy->properties["attack"] = to_string(10);
    dummy->useLater("PlainAttack", {dummy->id});
    EXPECT_EQ(dummy->properties["hp"], to_string(50));
    g->oneRoundRun();
    EXPECT_EQ(dummy->properties["hp"], to_string(40));
    g->oneRoundRun();
    EXPECT_EQ(dummy->properties["hp"], to_string(40));
}


TEST(basic_test, cd_test) {
    unique_ptr<Game> g(new Game());
    Entity *dummy = g->entityManager.get<entity::Dummy>();
    dynamic_cast<CoolDownSkill*>(dummy->skillList["PlainAttack"])->cd = 2;
    dummy->properties["attack"] = to_string(10);

    EXPECT_EQ(dummy->properties["hp"], to_string(50));

    dummy->useLater("PlainAttack", {dummy->id});
    g->oneRoundRun();
    EXPECT_EQ(dummy->properties["hp"], to_string(40));

    dummy->useLater("PlainAttack", {dummy->id});
    g->oneRoundRun();
    EXPECT_EQ(dummy->properties["hp"], to_string(40));

    dummy->useLater("PlainAttack", {dummy->id});
    g->oneRoundRun();
    EXPECT_EQ(dummy->properties["hp"], to_string(30));

}

TEST(basic_test, death_test) {
    unique_ptr<Game> g(new Game());
    Entity *dummy = g->entityManager.get<entity::Dummy>();
    dynamic_cast<CoolDownSkill*>(dummy->skillList["PlainAttack"])->cd = 0;
    dummy->properties["attack"] = dummy->properties["hp"];

    EXPECT_EQ(dummy->properties["hp"], to_string(50) );
    dummy->useLater("PlainAttack", {dummy->id});
    g->oneRoundRun();
    EXPECT_EQ(dummy->properties["hp"], to_string(0) );
    EXPECT_EQ(dummy->properties["dead"], "true");
}

TEST(basic_test, winner_test_1) {
    unique_ptr<Game> g(new Game());
	g->nowRound = MAX_ROUND;
	
    Entity *p1 = g->entityManager.get<entity::Plant>();
    p1->properties["team"] = "1";
    p1->properties["energy"] = "1";
    p1->properties["level"] = "0";
    EXPECT_EQ(g->getWinner(), 1);
	
    Entity *p2 = g->entityManager.get<entity::Plant>();
    p2->properties["team"] = "2";
    p2->properties["energy"] = "1";
    p2->properties["level"] = "0";
    EXPECT_EQ(g->getWinner(), -2);
	
	p2->properties["energy"] = "2";
    EXPECT_EQ(g->getWinner(), 2);
	
	p1->properties["level"] = "2";
    EXPECT_EQ(g->getWinner(), 1);
	
	p1->properties["energy"] = "0";
	p2->properties["energy"] = to_string(stats::getLevelUPConsumption(2) - stats::getLevelUPConsumption(0));
    EXPECT_EQ(g->getWinner(), -2);
	
	p1->properties["energy"] = "1";
    EXPECT_EQ(g->getWinner(), 1);
}

